package com.streamsets.datacollector.pileline.stage;

import com.streamsets.datacollector.bundles.Constants;
import com.streamsets.datacollector.model.etl.ColumnMappingConfigJson;
import com.streamsets.datacollector.model.jdbc.DestinationJdbcConfigJson;
import com.streamsets.datacollector.pileline.enums.SourceConfigEnum;
import com.streamsets.datacollector.restapi.bean.ConfigConfigurationJson;
import com.streamsets.datacollector.restapi.bean.StageConfigurationJson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 目标为关系型数据库(如mysql)的的目标节点配置
 * @author wengzhonghui
 * @date 2019/10/16 19:12
 */
public class InitTargetOfJdbcDbConfig {

    /*
     * 数据源stage实例名称
     */
    public static final String INSTANCE_NAME = "g0JDBC_01";

    /*
     * 目标数据库名
     */
    public static final String TARGET_SCHEMA_NAME = "schema";

    /*
     * 目标数据表名
     */
    public static final String TARGET_TABLE_NAME = "tableNameTemplate";

    /*
     * 列映射节点名
     */
    public static final String EXPORT_COLUMN_NAME_MAPPING = "columnNames";

	/*
	 * 初始化属性
	 * @param [stage, sourceJdbcConfig, exportMappingConfigJson, opType：import 或 export]
	 * @return
	 */
    public static void init(StageConfigurationJson stage, DestinationJdbcConfigJson sourceJdbcConfig, ColumnMappingConfigJson exportMappingConfigJson){
        if(INSTANCE_NAME.equalsIgnoreCase(stage.getInstanceName())) {
            if (sourceJdbcConfig != null) {
                List<ConfigConfigurationJson> configs =
                    intConfig(stage.getConfiguration(), sourceJdbcConfig, exportMappingConfigJson);
                stage.setConfiguration(configs);
            }
        }
    }

    public static List<ConfigConfigurationJson> intConfig(List<ConfigConfigurationJson> oldConfigs, DestinationJdbcConfigJson sourceJdbcConfig
            , ColumnMappingConfigJson exportMappingConfigJson){
        List<ConfigConfigurationJson> newConfigs = new ArrayList<>();
        for(ConfigConfigurationJson config :oldConfigs ){
            config = initConnectionString(config, sourceJdbcConfig);
            config = initUserName(config, sourceJdbcConfig);
            config = initPassword(config, sourceJdbcConfig);
            config = initDriverClassName(config, sourceJdbcConfig);
            config = initExportSchema(config, sourceJdbcConfig);
            config = initTargetTable(config, sourceJdbcConfig);
            if(exportMappingConfigJson != null){
                config = initNameMappingConfigs(config, exportMappingConfigJson.getNameMappingColumns());
            }
            newConfigs.add(config);
        }
        return newConfigs;
    }


    /*
     * 初始化导出列映射配置
     * 格式示例如：
     * [ {
          "paramValue" : "?",
          "dataType" : "USE_COLUMN_TYPE",
          "columnName" : "id",
          "field" : "/id",
          "defaultValue" : "-1"
        }, {
          "paramValue" : "?",
          "dataType" : "USE_COLUMN_TYPE",
          "field" : "/created_dt",
          "columnName" : "created_dt",
          "defaultValue" : ""
        } ]
     * @param [config 原来的配置, columnNames:需要映射的列名列表]
     * @return 返回新生成的配置
     */
    private static ConfigConfigurationJson initNameMappingConfigs(ConfigConfigurationJson config, List<Map<String, String>> nameMappingColumns){
        if(EXPORT_COLUMN_NAME_MAPPING.equalsIgnoreCase(config.getName())){
            List configValList = new ArrayList();
            if(nameMappingColumns != null && nameMappingColumns.size() > 0){
                for(Map<String, String> valConfig : nameMappingColumns){
                    String defaultValue = valConfig.get("defaultValue") == null ?"" : valConfig.get("defaultValue");
                    Map data = new HashMap();
                    data.put("paramValue","?");
                    data.put("dataType","USE_COLUMN_TYPE");
                    data.put("defaultValue",defaultValue);

                    data.put("field", "/" + valConfig.get("sourceColumn"));
                    data.put("columnName", valConfig.get("targetColumn"));
                    configValList.add(data);
                }
            }
            config = new ConfigConfigurationJson(EXPORT_COLUMN_NAME_MAPPING  ,configValList);
        }
        return config;
    }



    // 初始化导出数据库名
    private static ConfigConfigurationJson initExportSchema(ConfigConfigurationJson config, DestinationJdbcConfigJson sourceJdbcConfig){
        if(TARGET_SCHEMA_NAME.equalsIgnoreCase(config.getName())){
            config = new ConfigConfigurationJson(TARGET_SCHEMA_NAME
                    ,sourceJdbcConfig.getTargetSchemaName());
        }
        return config;
    }

    // 初始化导出表名
    private static ConfigConfigurationJson initTargetTable(ConfigConfigurationJson config, DestinationJdbcConfigJson sourceJdbcConfig){
        if(TARGET_TABLE_NAME.equalsIgnoreCase(config.getName())){
            // 数据导入或导出的目标表
            String tableInc = "tmp_"  + sourceJdbcConfig.getTargetTable();
            config = new ConfigConfigurationJson(TARGET_TABLE_NAME, tableInc);
        }
        return config;
    }

    // 初始化数据库连接
    private static ConfigConfigurationJson initConnectionString(ConfigConfigurationJson config, DestinationJdbcConfigJson sourceJdbcConfig){

        if(SourceConfigEnum.jdbcConnectionString.getName().equalsIgnoreCase(config.getName())){
            config = new ConfigConfigurationJson(SourceConfigEnum.jdbcConnectionString.getName()
                    ,sourceJdbcConfig.getConnectionString());
        }
        return config;
    }

    // 初始化数据库用户名
    private static ConfigConfigurationJson initUserName(ConfigConfigurationJson config, DestinationJdbcConfigJson sourceJdbcConfig){

        if(SourceConfigEnum.username.getName().equalsIgnoreCase(config.getName())){
            config = new ConfigConfigurationJson(SourceConfigEnum.username.getName()
                    ,sourceJdbcConfig.getJdbcUserName());
        }
        return config;
    }

    // 初始化数据库密码
    private static ConfigConfigurationJson initPassword(ConfigConfigurationJson config, DestinationJdbcConfigJson sourceJdbcConfig){

        if(SourceConfigEnum.password.getName().equalsIgnoreCase(config.getName())){
            config = new ConfigConfigurationJson(SourceConfigEnum.password.getName()
                    ,sourceJdbcConfig.getJdbcPassword());
        }
        return config;
    }

    // 初始化数据库驱动
    private static ConfigConfigurationJson initDriverClassName(ConfigConfigurationJson config, DestinationJdbcConfigJson sourceJdbcConfig){

        if(SourceConfigEnum.driverClassName.getName().equalsIgnoreCase(config.getName())){
            config = new ConfigConfigurationJson(SourceConfigEnum.driverClassName.getName()
                    ,sourceJdbcConfig.getDriverClassName());
        }
        return config;
    }

}
